package com.situ.gym.controller;

import com.situ.gym.pojo.vo.ChartCountVO;
import com.situ.gym.pojo.vo.DayCountVO;
import com.situ.gym.service.ICoachService;
import com.situ.gym.service.ICourseService;
import com.situ.gym.service.IReserveService;
import com.situ.gym.service.IUserService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chart")
public class ChartController {
    @Autowired
    private ICoachService coachService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IReserveService reserveService;
    @Autowired
    private ICourseService courseService;

    @GetMapping("/selectCoachCount")
    public Result<List<ChartCountVO>> selectCoachCount() {
        List<ChartCountVO> list = coachService.selectCoachCount();
        return Result.ok(list);
    }

    @GetMapping("/selectUserTotal")
    public Result<Long> selectUserTotal() {
        long count = userService.count();
        return Result.ok(count);
    }

    @GetMapping("/selectCoachTotal")
    public Result<Long> selectCoachTotal() {
        long count = coachService.count();
        return Result.ok(count);
    }


    @GetMapping("/selectReserveTotal")
    public Result<Long> selectReserveTotal() {
        long count = reserveService.count();
        return Result.ok(count);
    }

    @GetMapping("/selectCourseTotal")
    public Result<Long> selectCourseTotal() {
        long count = courseService.count();
        return Result.ok(count);
    }


    @GetMapping("/selectHotCourse")
    public Result<List<ChartCountVO>> selectHotCourse() {
        List<ChartCountVO> list = courseService.selectHotCourse();
        return Result.ok(list);
    }


    @GetMapping("/selectUserCount")
    public Result<List<DayCountVO>> selectUserCount(@RequestParam("date") String dateStr) {
        try {
            // 将字符串转换为Date对象
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            List<DayCountVO> list = userService.selectUserCount(date);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error("日期格式错误");
        }
    }

    @GetMapping("/selectReserveCount")
    public Result<List<DayCountVO>> selectReserveCount(@RequestParam("date") String dateStr) {
        try {
            // 将字符串转换为Date对象
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            List<DayCountVO> list = reserveService.selectReserveCount(date);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error("日期格式错误");
        }
    }

    // 用户个人预约趋势
    @GetMapping("/selectUserPersonalReserve")
    public Result<List<DayCountVO>> selectUserPersonalReserve(@RequestParam("date") String dateStr, @RequestParam("userId") Integer userId) {
        try {
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            List<DayCountVO> list = reserveService.selectUserPersonalReserve(date, userId);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error("日期格式错误");
        }
    }

    // 用户个人预约总数
    @GetMapping("/selectUserPersonalReserveTotal")
    public Result<Long> selectUserPersonalReserveTotal(@RequestParam("userId") Integer userId) {
        long count = reserveService.selectUserPersonalReserveTotal(userId);
        return Result.ok(count);
    }

    // 用户课程总数
    @GetMapping("/selectUserCourseTotal")
    public Result<Long> selectUserCourseTotal(@RequestParam("userId") Integer userId) {
        long count = courseService.selectUserCourseTotal(userId);
        return Result.ok(count);
    }

    // 用户活跃天数
    @GetMapping("/selectUserActiveDays")
    public Result<Long> selectUserActiveDays(@RequestParam("userId") Integer userId) {
        long count = userService.selectUserActiveDays(userId);
        return Result.ok(count);
    }

    // 用户喜欢的教练
    @GetMapping("/selectUserFavoriteCoach")
    public Result<List<ChartCountVO>> selectUserFavoriteCoach(@RequestParam("userId") Integer userId) {
        List<ChartCountVO> list = coachService.selectUserFavoriteCoach(userId);
        return Result.ok(list);
    }

    // 教练个人预约趋势
    @GetMapping("/selectCoachPersonalReserve")
    public Result<List<DayCountVO>> selectCoachPersonalReserve(@RequestParam("date") String dateStr, @RequestParam("coachId") Integer coachId) {
        try {
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            List<DayCountVO> list = reserveService.selectCoachPersonalReserve(date, coachId);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error("日期格式错误");
        }
    }

    // 教练个人预约总数
    @GetMapping("/selectCoachReserveTotal")
    public Result<Long> selectCoachReserveTotal(@RequestParam("coachId") Integer coachId) {
        long count = reserveService.selectCoachReserveTotal(coachId);
        return Result.ok(count);
    }

    // 教练课程总数
    @GetMapping("/selectCoachCourseTotal")
    public Result<Long> selectCoachCourseTotal(@RequestParam("coachId") Integer coachId) {
        long count = courseService.selectCoachCourseTotal(coachId);
        return Result.ok(count);
    }

    // 教练活跃天数
    @GetMapping("/selectCoachActiveDays")
    public Result<Long> selectCoachActiveDays(@RequestParam("coachId") Integer coachId) {
        long count = coachService.selectCoachActiveDays(coachId);
        return Result.ok(count);
    }
}